import { isNull, isNumber, isObject, isString, isArray } from "@/unit/tool";

const columns = [
  {
    title: "上传时间",
    width: "180px",
    dataIndex: "uploadTime",
    ellipsis: false,
    ellipsis: true
  },
  {
    title: "客户名",
    dataIndex: "customerName",
    width: "180px",
    ellipsis: false,
    ellipsis: true
  },
  {
    title: "合同号",
    dataIndex: "contractCode",
    width: "120px",
    ellipsis: false,
    ellipsis: true
  },
  {
    title: "登录公司",
    dataIndex: "company",
    width: "150px",
    ellipsis: false,
    ellipsis: true
  },
  {
    title: "系统编号",
    dataIndex: "macNO",
    width: "160px",
    ellipsis: false,
    ellipsis: true
  },
  {
    title: "数控系统型号",
    dataIndex: "macModel",
    width: "160px",
    ellipsis: false,
    ellipsis: true
  },
  {
    title: "SN号",
    dataIndex: "macSN",
    width: "160px",
    ellipsis: false,
    ellipsis: true
  },
  {
    title: "IPC编号",
    dataIndex: "ipcCode",
    width: "180px",
    ellipsis: false,
    ellipsis: true
  }
];

const versionColumns = [
  {
    title: "版本检测",
    dataIndex: "itemName",
    ellipsis: false
  },
  {
    title: "标准值",
    dataIndex: "standard",
    ellipsis: false
  },
  {
    title: "检测值",
    dataIndex: "check",
    ellipsis: false
  },
  {
    title: "检查结果",
    dataIndex: "isEqueal",
    ellipsis: false,
    scopedSlots: { customRender: "isEqueal" }
  }
];

const motorColumns = [
  {
    title: "特征值",
    dataIndex: "name",
    ellipsis: false
  },
  {
    title: "轴0",
    dataIndex: "axis0",
    ellipsis: false
  },
  {
    title: "轴1",
    dataIndex: "axis1",
    ellipsis: false
  },
  {
    title: "轴2",
    dataIndex: "axis2",
    ellipsis: false
  }
];

const servosColumns = [
  {
    title: "名称",
    dataIndex: "axisName",
    ellipsis: false,
    disabled: true,
    scopedSlots: { customRender: "axisName" }
  },
  {
    title: "伺服驱动型号",
    dataIndex: "servoModel",
    ellipsis: false,
    scopedSlots: { customRender: "servoModel" }
  },
  {
    title: "伺服驱动编号",
    dataIndex: "servoId",
    ellipsis: false,
    scopedSlots: { customRender: "servoId" }
  },
  {
    title: "伺服生产日期",
    dataIndex: "servoProductTime",
    ellipsis: false,
    type: "month",
    scopedSlots: { customRender: "servoProductTime" }
  },
  {
    title: "电机型号",
    dataIndex: "elecModel",
    ellipsis: false,
    scopedSlots: { customRender: "elecModel" }
  },
  {
    title: "电机编号",
    dataIndex: "elecId",
    ellipsis: false,
    width: "auto",
    scopedSlots: { customRender: "elecId" }
  }
];

const formOptions = [
  { value: "uploadTime", label: "记录上传时间", disabled: true },
  { value: "loginName", label: "检测人员", disabled: true },
  {
    value: "passFlag",
    label: "检测是否合格",
    disabled: true,
    type: "select",
    options: [
      { value: 1, label: "不合格" },
      { value: 0, label: "合格" }
    ]
  },
  { value: "contractCode", label: "合同号" },
  { value: "customerName", label: "客户名" },
  { value: "macNO", label: "数控系统编号" },
  { value: "macModel", label: "数控系统型号" },
  { value: "macProductTime", label: "成套系统出厂日期", disabled: true },
  { value: "ipcCode", label: "IPC编号" },
  { value: "macSN", label: "数控系统SN号", disabled: true },
  { value: "sysProductTime", label: "数控系统生产时间", disabled: true },
  {
    value: "partCheckFlag",
    label: "是否散件上传",
    disabled: true,
    type: "select",
    options: [
      { value: 1, label: "是" },
      { value: 0, label: "否" }
    ]
  },
  { value: "ioUnitType", label: "总线式I/0单元型号" },
  { value: "ioUnitId", label: "I/0单元编号" },
  {value: "servos", type: "table",
  options: servosColumns
}
];

function buildTableData(lists) {
  // let { afterSaleService, appointor, evaluation, repairList, suit } = res;
  let newLists = [];
  lists.forEach((item, index) => {
    newLists.push(buildListsData(item, index));
  });
  return newLists;
}

function buildListsData(item, index) {
  let newRes = { key: index };
  for (let key in item) {
    if (Object.hasOwnProperty.call(item, key)) {
      let obj = item[key];
      if (!isNull(obj)) {
        newRes = Object.assign(newRes, resolveParams(obj, key));
      }
    }
  }
  return newRes;
}

function resolveParams(val, key) {
  if (isNumber(val)) {
    return { [key]: val };
  } else if (isString(val)) {
    return { [key]: val };
  } else if (isArray(val)) {
    return { [key]: val };
  } else if (isObject(val)) {
    let { id, ...restVal } = val;
    return { ...restVal };
  }
}

export { columns, buildTableData, formOptions, versionColumns,motorColumns };
